package com.inter.insideinterservice;

import com.inter.entity.common.OrderPaymentManager;
import com.inter.entity.pojo.Constant;
import com.inter.entity.pojo.request.RequestBody.OrderRefuelingPackageRequestBody;
import com.inter.entity.pojo.request.RequestHeader;
import com.inter.entity.pojo.request.subRequest.OrderRefuelingPackageRequest;
import com.inter.entity.pojo.response.subResponse.OrderRefuelingPackageResponse;
import com.inter.utils.HttpClientTool;
import com.inter.utils.JaxbUtil;

/***
 * 去北京订购加油包的实现类
 */

public class TrunToOrderRefuelingPackageService {

    public OrderPaymentManager orderfuelingPackage(String partnerCode, String refpackageName, String refpackageNode, String deviceType, String orderId, String secret) {
        try {
            OrderRefuelingPackageRequestBody body = new OrderRefuelingPackageRequestBody();
            body.setOrderId(orderId);
            body.setDeviceType(deviceType);
            body.setRefpackageName(refpackageName);
            body.setRefpackageCode(refpackageNode);

            // 请求参数拼接
            OrderRefuelingPackageRequest request = new OrderRefuelingPackageRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();
            RequestHeader header = utils.getRequestHeader(Constant.cmd_orderfuelingPackage, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(OrderRefuelingPackageRequest.class).toXml(request, "utf-8");
            //System.out.println("请求报文：" + sRequest);

            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            //System.out.println("响应报文：" + result);
            OrderRefuelingPackageResponse responseObj = new JaxbUtil(OrderRefuelingPackageResponse.class).fromXml(result, "utf-8");
            String retCode = responseObj.getHeader().getResultCode();
            String resultInfo = responseObj.getHeader().getResultInfo();
            //因为回复的body为空，只需要把retCode和resultInfo回传回去。
            OrderPaymentManager user = new OrderPaymentManager();
            user.setRetCode(retCode);
            user.setResultInfo(resultInfo);
            return user;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
